﻿using Core.Audit;
using Core.Audit.Features.Queries;
using Core.Identity.Menus.Features.Queries.GetAll;
using Core.Models;
using Infrastructure.Auth.Policy;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;

namespace Infrastructure.Identity.Audit.Endpoints;

public static class AuditTrailEndpoints
{
    /// <summary>
    /// 审计日志 API
    /// </summary>
    public static IEndpointRouteBuilder MapAuditTrailEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/api/audittrail")
                       .WithTags("AuditTrail");

        // GET /api/audit-trail/page
        group.MapGet("/page", QueryAuditTrailsAsync)
             .WithDescription("分页查询")
             .RequirePermission("AuditTrail:View:Page")
             .WithMetadata(new EndpointNameMetadata(nameof(QueryAuditTrailsAsync)));

        return app;
    }

    /// <summary>
    /// 分页查询审计日志
    /// </summary>
    private static async Task<Result<PaginatedData<AuditTrailDto>>> QueryAuditTrailsAsync(
        [AsParameters] AuditTrailsWithPaginationQuery request,
        [FromServices] IMediator mediator)
    {
        var data = await mediator.Send(request);
        return await Result<PaginatedData<AuditTrailDto>>.SucceedAsync(data);
    }
}
